/*
 * Copyright 2012 Clement Sannicolo <clement.sannicolo@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Ext.define('Nutrient.controller.Food',
{
	extend: 'Ext.ux.app.Controller',

	stores: [
	    'Nutrient.store.Search',
	    'Nutrient.store.Food'
	],
	models: [
	    'Nutrient.model.Search',
	    'Nutrient.model.Food'
	],
	views: [
	    'Nutrient.view.food.Add',
	    'Nutrient.view.food.Edit'
	],

	init: function()
	{
		this.control({
			'#nutrient-food-filter': {
				search: function(filter, query) {
					this.search(query);
				}
			}
		});
	},

	search: function(query)
	{
		var store = App.getStore('Nutrient.store.Search');
		if (query == '') {
			store.removeAll();
		}
		else {
			store.load({params: {
				name: query
			}});
		}
	},

	show: function(id)
	{
		var store = App.getStore('Nutrient.store.Food'),
			panel = Ext.getCmp('nutrient-food-show');

		store.loadById(id, {
			success: function(record) {
				panel.loadRecord(record);
			}
		});
	},

	add: function()
	{
		if (!this.addWindow) {
			this.addWindow = Ext.create('Nutrient.view.food.Add', {});
		}
		this.addWindow.show();
	},
	
	create: function(values)
	{
		var store = App.getStore('Nutrient.store.Food'),
			record = Ext.create('Nutrient.model.Food', values);

		store.add(record);
		store.sync();
	},

	edit: function(id)
	{
		var store = App.getStore('Nutrient.store.Food');

		store.loadById(id, {
			scope: this,
			success: function(record) {
				if (!this.editWindow) {
					this.editWindow = Ext.create('Nutrient.view.food.Edit', {});
				}
				this.editWindow.loadRecord(record);
				this.editWindow.show();
			}
		});
	},

	update: function(record, values)
	{
		var store = App.getStore('Nutrient.store.Food');

		record.set(values);
		store.sync();
	},
	
	remove: function(id)
	{
		var store = App.getStore('Nutrient.store.Food');

		store.loadById(id, {
			scope: this,
			success: function(record) {
				Ext.Msg.show({
					title:'Delete',
					msg: 'Do you really want to delete "' + record.get('name') + '" ?',
					buttons: Ext.Msg.YESNO,
					icon: Ext.Msg.QUESTION,
					scope: this,
					fn: function(buttonId)
					{
						if (buttonId == 'yes') {
							this.destroy(record);
						}
					}
				});
			}
		});
	},

	destroy: function(record)
	{
		var store = App.getStore('Nutrient.store.Food');

		store.remove(record);
		store.sync();
	}
});